/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import model.Comentarios;
import dao.ComentariosDao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;

/**
 *
 * @author rafael
 */
public class ComentariosDaoImpl implements ComentariosDao {

    @Override
    public void inserir(Comentarios comentarios) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(comentarios);
            session.beginTransaction().commit();
            GrowMessages.infoMessage("Comentario inserido com sucesso", "Ok");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            System.out.println(e.getLocalizedMessage());
            GrowMessages.errorMessage("Erro ao inserir comentário", "Erro");
        } finally {
            session.close();
        }
    }

    @Override
    public void eliminar(Comentarios comentarios) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(comentarios);
            session.beginTransaction().commit();
            GrowMessages.infoMessage("Comentario apagado com sucesso", "Ok");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            System.out.println(e.getLocalizedMessage());

        } finally {
            session.close();
        }

    }

    @Override
    public List<Comentarios> selectAll(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Comentarios.class);
        criteria.add(Restrictions.eq("veiculos.pkVeiculos", id));
        try {
            return criteria.list();
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public Comentarios selectByIdVeiculo(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Comentarios.class);
        criteria.add(Restrictions.eq("veiculos.pkVeiculos", id));
        try {
            return (Comentarios) criteria.uniqueResult();
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public List<Comentarios> selectByIdUsuario(Integer FK_COMENTARIOS_USUARIOS) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Comentarios.class);
        criteria.add(Restrictions.eq("usuarios.pkId", FK_COMENTARIOS_USUARIOS));
        try {
            return criteria.list();
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }
}
